<template>
	<div>
		<div class="page-index" v-if="!pageLoading">
			<div class="hd">
				<div class="info">
					<div class="info-hd">付款给</div>
					<div class="info-bd">商丘市立医院</div>
				</div>
				<div class="info-icon">
					<image src="../../static/info-icon.png" class="info-icon-img"></image>
					<!-- <img class="info-icon-img" src="../../" alt=""> -->
				</div>
				<div class="bg"></div>
			</div>
			<div class="bd">
				<div class="box">
					<div class="box-hd">
						<div class="box-hd-label">费用总额</div>
						<div class="box-hd-value">{{resData.ZongJinE}}元</div>
					</div>
					<div class="box-bd">
						<div class="box-item">
							<div class="box-bd-label">医保支付</div>
							<div class="box-bd-value">{{resData.YiBaoZf}}元</div>
						</div>
						<div class="box-item">
							<div class="box-bd-label">个人帐户支付</div>
							<div class="box-bd-value">{{resData.GeRenZhZc}}元</div>
						</div>
						<div class="box-item">
							<div class="box-bd-label">其他抵扣金额</div>
							<!-- <div class="box-bd-value">{{resData.YouHuiJe}}元</div> -->
							<div class="box-bd-value">0.00元</div>
						</div>
					</div>
					<div class="box-ft">
						<div class="box-ft-label">现金支付</div>
						<div class="box-ft-value">{{resData.YingShouJe}}元</div>
					</div>
					<div class="box-append" @click="handleActionsheet">
						查看明细
					</div>
				</div>
				<div class="bd-append">
					<i class="bd-append-icon"></i>
					<div class="bd-append-text">医保移动支付</div>
				</div>
			</div>
			<div class="ft">
				<div class="pay">
					<div class="pay-label">您还需支付：</div>
					<div class="pay-value">¥{{resData.YingShouJe}}</div>
				</div>
				<div class="btn" @click="wxPay">去支付</div>
			</div>
			<div class="actionsheet" v-show="actionsheetVisible">
				<div class="actionsheet-mask" @click="handleActionsheet"></div>
				<div class="actionsheet-panel">
					<div class="actionsheet-hd">
						<div class="actionsheet-hd-tt">处方明细</div>
						<i class="actionsheet-icon-close" @click="handleActionsheet"></i>
					</div>
					<div class="actionsheet-bd">
						<div class="actionsheet-box" v-for="item in prescriptionList" :key="item.title">
							<div class="actionsheet-box-hd">{{item.title}}</div>
							<div class="actionsheet-box-bd">
								<div class="actionsheet-box-item" v-for="boxItem in item.list" :key="boxItem.label">
									<div class="actionsheet-box-item-label-wrap">
										<div class="actionsheet-box-item-label">{{boxItem.label}}</div>
										<div class="actionsheet-box-item-sublabel">{{boxItem.subLabel}}</div>
									</div>
									<div class="actionsheet-box-item-value" :class="[boxItem.highlight ? 'em' : '']">
										{{boxItem.value}}
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<u-loading-page :loading="pageLoading" loading-text="加载中..." fontSize="15"></u-loading-page>
	</div>

</template>

<script>
	import {
		ybApi,
		insuranceApi,
		createApi,
		authinfoApi
	} from "@/api/user"
	export default {
		data() {
			return {
				payData: {},
				pageLoading: true,
				authCode: "",
				prescriptionList: [{
						title: '就诊信息',
						list: [{
								label: '门诊科室',
								value: ''
							},
							{
								label: '医生姓名',
								value: ''
							},
							{
								label: '处方时间',
								value: ''
							},
							{
								label: '费用总额',
								value: '',
								highlight: true
							},
						]
					},
					{
						title: '费用信息',
						list: []
					},
				],
				actionsheetVisible: false,
				resData: {},
			}
		},
		onLoad(option) {
			// uni.showModal({
			// 	title: '提示',
			// 	content: location.href,
			// })
			// #ifdef H5
			if (option.authCode) {
				this.authCode = option.authCode
				this.getYb()
				if (option.yibaoData) {
					this.payData = JSON.parse(option.yibaoData)
					this.prescriptionList[0].list[0].value = this.payData.admDept
					this.prescriptionList[0].list[1].value = this.payData.admDoctor
					this.prescriptionList[0].list[2].value = this.payData.admDateTime
					this.prescriptionList[0].list[3].value = this.payData.admTotalMoney + '元'
					this.payData.itemList.forEach((item) => {
						this.prescriptionList[1].list.push({
							label: item.itemName,
							// subLabel: item.itemCount,
							value: item.itemTotalMoney + '元',
						})
					})
				}
			} else {
				uni.redirectTo({
					url: '/pages/paymentreslut/paymentreslut?item=' + encodeURIComponent(JSON.stringify(this
						.payData))
				})
			}
			// #endif 
			// #ifdef MP-ALIPAY
			// uni.showModal({
			// 	title: '提示',
			// 	content: JSON.stringify(option),
			// })
			console.log(123456, option);
			this.payData = JSON.parse(uni.getStorageSync('yibaoData'))
			if (option.resultCode && option.resultCode == 'SUCCESS') {
				uni.getAuthCode({
					scopes: ['nhsamp', 'auth_user'],
					success: async (res) => {
						const resa = await authinfoApi({
							authCode: res.authCode
						})
					}
				})
			}
			if (option.resultCode && option.resultCode != 'SUCCESS') {
				uni.redirectTo({
					url: '/pages/paymentreslut/paymentreslut?item=' + encodeURIComponent(JSON.stringify(this
						.payData))
				})
			} else {
				if (uni.getStorageSync('yibaoData')) {
					// this.payData = JSON.parse(uni.getStorageSync('yibaoData'))
					// console.log(this.payData);
					this.prescriptionList[0].list[0].value = this.payData.admDept
					this.prescriptionList[0].list[1].value = this.payData.admDoctor
					this.prescriptionList[0].list[2].value = this.payData.admDateTime
					this.prescriptionList[0].list[3].value = this.payData.admTotalMoney + '元'
					this.payData.itemList.forEach((item) => {
						this.prescriptionList[1].list.push({
							label: item.itemName,
							// subLabel: item.itemCount,
							value: item.itemTotalMoney + '元',
						})
					})
					this.getPay()
				}
			}

			// #endif 


			// uni.showModal({
			// 	title: '提示',
			// 	content: option.yibaoData,
			// })

		},
		methods: {
			handleActionsheet() {
				this.actionsheetVisible = !this.actionsheetVisible;
			},
			async getPay() {
				const res = await insuranceApi({
					id: this.payData.id,
					type: 3,
					money: this.payData.admTotalMoney,
					receiptNo: this.payData.receiptNo,
					// #ifdef MP-ALIPAY
					payCardTypeId: '26',
					// #endif 
					// #ifdef H5
					payCardTypeId: '27',
					// #endif 
				})
				this.resData = res.data
				const resa = JSON.stringify(res.data)
				this.pageLoading = false
				// uni.showModal({
				// 	title: '提示',
				// 	content: res.data.GeRenZhZc,
				// })
			},
			async getYb() {
				this.pageLoading = true
				const res = await ybApi({
					authCode: this.authCode
				})
				this.getPay()
				// uni.showModal({
				// 	title: '提示',
				// 	content: res.data,
				// })
			},
			async wxPay() {

				try {

					uni.showLoading({
						title: '缴费中...',
						mask: true
					})
					const res = await createApi({
						id: this.payData.id,
						type: 3,
						money: this.payData.admTotalMoney,
						receiptNo: this.payData.receiptNo,
						// #ifdef MP-ALIPAY
						payCardTypeId: '26',
						// #endif 
						// #ifdef H5
						payCardTypeId: '27',
						// #endif 
					})
					uni.hideLoading()
					// uni.showModal({
					// 	title: '提示',
					// 	content: '这是一个模态弹窗' + res.msg,
					// })
					// #ifdef H5
					const msg = JSON.parse(res.msg)
					if (msg.err_code == 0) {
						// window.open(msg.pay_url)
						window.location.href = msg.pay_url
					} else {
						uni.showToast({
							title: msg.err_code_des,
							icon: 'none',
							duration: 1500
						})
					}
					// #endif 
					// #ifdef MP-ALIPAY
					console.log(res.data.data.body);
					my.tradePay({
						orderStr: res.data.data.body, // JAVA服务端SDK生成APP支付订单信息
						success: (res) => {
							console.log(987, res);
							if (res.resultCode == 9000) {
								uni.redirectTo({
									url: '/pages/paysucceed/paysucceed'
								})
							} else if (res.resultCode == 6001) {
								uni.$u.toast('取消支付')
							} else {
								uni.$u.toast('支付失败')
							}

						},
						fail: (res) => {
							uni.$u.toast('缴费失败')
						}
					});

					// #endif 

					// this.wxpayOrder(JSON.parse(res))
				} catch (e) {
					// uni.showModal({
					// 	title: '提示',
					// 	content: '这是一个模态弹窗12',
					// })
					uni.hideLoading()
				}

			},
			wxpayOrder(data) {
				if (typeof WeixinJSBridge === 'undefined') {
					if (document.addEventListener) {
						document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady(data), false)
					} else if (document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady(data))
						document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady(data))
					}
				} else {
					this.onBridgeReady(data)
				}
			},
			onBridgeReady(etr) {
				let that = this
				WeixinJSBridge.invoke( //这个对象只有微信浏览器支持
					'getBrandWCPayRequest', etr,
					function(res) {
						if (res.err_msg == 'get_brand_wcpay_request:ok') {
							uni.navigateTo({
								url: '/pages/paysucceed/paysucceed'
							})
						} else {
							uni.$u.toast('缴费失败')
						}
					})

			},
		}
	}
</script>

<style lang="scss" scoped>
	@import './index.css';
	@import './actionsheet.css';
</style>